//
//  WQWelcomeView.swift
//  WeiBo
//
//  Created by 张德华 on 7/24/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit
import SDWebImage
class WQWelcomeView: UIView {
    
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
        self.backgroundColor = UIColor.redColor()
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit{
        print("\(classForCoder) \(#function)")
    }
    
    // MARK: - ************添加到窗口
    override func didMoveToWindow() {
        
        ///------ 更新约束
        avatarImageView.snp_updateConstraints { (make) in
            make.centerY.equalTo(self).offset(-100)
        }
        
        ///------ 动画
        UIView.animateWithDuration(1, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4, options: [], animations: {
            self.layoutIfNeeded()
        }) { (_) in
            
            UIView.animateWithDuration(0.5, animations: {
                self.welcomeLabel.alpha = 1
                }, completion: { (_) in
                    self.removeFromSuperview()
            })
            
            
//            self.performSelector(#selector(UIView.removeFromSuperview), withObject: nil, afterDelay: 1)
            
        }
        
    }
    
    // MARK: - ************私有控件
    private lazy var avatarImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var welcomeLabel = UILabel(wq_text: "欢迎回来", fontSize: 15, textColor: UIColor.darkGrayColor())
    
    
    
}


extension WQWelcomeView {
    
    func setupUI() {
        
        ///------ 设置背景图片
        backgroundColor = UIColor(patternImage: UIImage(named: "ad_background")!)
        
        ///------ 添加控件
        addSubview(avatarImageView)
        addSubview(welcomeLabel)
        
        welcomeLabel.alpha = 0
        
        ///------ 头像剪切
        let iconWH: CGFloat = 45
        avatarImageView.layer.cornerRadius = iconWH
        avatarImageView.clipsToBounds = true
        
        ///------ 添加约束
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-40)
            make.size.equalTo(CGSize(width: iconWH * 2, height: iconWH * 2))
        }
        welcomeLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(avatarImageView.snp_bottom).offset(10)
        }
        
        layoutIfNeeded()
        
        ///------ 设置头像
        avatarImageView.wq_setImage(WQUserAccountModel.sharedUserAccount.avatar_large, placeholderName: "ad_background")
        
    }
    
    
    
    
    
    
    
    
}